package fi.vamk.android;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Classwork extends ListActivity {
	TextView selection;
	private String[] stringCodeArray; 
	private String[] stringTitleArray;
	private String[] stringClassworkArray;
	private String[] stringStartTimeArray;
	private String[] stringEndTimeArray;
	@Override
    public void onCreate(Bundle icicle) {
    	super.onCreate(icicle);
        setContentView(R.layout.classwork);
       
        		
        String jsonString = menuList.getClasswork; 
        JSONArray json = null;
        try {
            json = new JSONArray(jsonString);
        } catch (JSONException je) {
            Log.e("TAG", "Json Exception" + je.getMessage() );
            return;
        }
     JSONObject jsonObject = null;
     ArrayList<String> arrayCodeList = new ArrayList<String>();
     ArrayList<String> arrayTitleList = new ArrayList<String>();
     ArrayList<String> arrayClassworkList = new ArrayList<String>();
     ArrayList<String> arrayStartTimeList = new ArrayList<String>();
     ArrayList<String> arrayEndTImeList = new ArrayList<String>();
     for (int i = 0; i < json.length(); i++) {
            try {
                jsonObject = json.getJSONObject(i);
                    if(jsonObject.has("title"))
                     {
                       String code = jsonObject.getString("code");
                       String title = jsonObject.getString("title");
                       String classwork = jsonObject.getString("classwork");
                       String startTime = jsonObject.getString("start_time");
                       String endTIme = jsonObject.getString("end_time");
                       arrayCodeList.add(code);
                       arrayTitleList.add(title);
                       arrayClassworkList.add(classwork);
                       arrayStartTimeList.add(startTime);
                       arrayEndTImeList.add(endTIme);
                     }     

                 
            } catch (JSONException jee) {
                Log.e("TAG", "" + jee.getMessage());
            }

        }
        stringCodeArray = arrayCodeList.toArray(new String[arrayCodeList.size()]);
        stringTitleArray = arrayTitleList.toArray(new String[arrayTitleList.size()]);
        stringClassworkArray = arrayClassworkList.toArray(new String[arrayClassworkList.size()]);
        stringStartTimeArray = arrayStartTimeList.toArray(new String[arrayStartTimeList.size()]);
        stringEndTimeArray = arrayEndTImeList.toArray(new String[arrayEndTImeList.size()]);
        try {
	    	 
	    	 setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringCodeArray));
	        		 selection=(TextView)findViewById(R.id.selection);
		} catch (Exception e) {
			// TODO: handle exception
			alertOkOnly("Exception!"  , e.toString());
		}
    
    }
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.layout.menu_button, menu);
	        return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Handle item selection
	        switch (item.getItemId()) {
	        case R.id.exit:
	        	finish();
	            return true;
	        case R.id.list:
	        	startActivity(new Intent(Classwork.this, menuList.class));
	         	finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
     public void onListItemClick( ListView parent, View v, int position, long id) {
     		try {
     			//selection.setText(name[position]);
     			alertOkOnly(stringCodeArray[position].toString()+ " - "  + stringTitleArray[position].toString(), stringClassworkArray[position].toString() + "  START TIME: " + stringStartTimeArray[position].toString() + "  END TIME: " + stringEndTimeArray[position].toString());
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    }
    
    public void alertOkOnly(String title, String message)
    {
    	AlertDialog.Builder builder=
        	new AlertDialog.Builder(Classwork.this);
        	builder
        	.setTitle(title)
        	.setMessage(message)
        	.setPositiveButton("OK", null)
        	.show();
    } 
    
    }
